package com.wanxi.servlet;

import com.alibaba.fastjson2.JSONObject;
import com.wanxi.model.UserModel;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(value = {"/login", "/lo"})
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        System.out.println(11);
//        String method = req.getMethod();
//        System.out.println(method);
//
//        System.out.println(req.getContextPath());;
//        System.out.println(req.getRequestURL());
//        System.out.println(req.getRequestURI());
//        System.out.println(req.getQueryString());
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        UserModel model = new UserModel(username, password);
        model.setUsername(username);
        System.out.println(model);
//        resp.getWriter().println(JSONObject.toJSONString(model));
//        req.setAttribute("user", username);
//        req.setAttribute("user", username);
//        req.getServletContext().setAttribute("user", username);
//        req.getServletContext().setAttribute("pass", password);
        req.getSession().setAttribute("user", username);
        req.getSession().setAttribute("pass", password);
//        map.put("user",username);
//        req.removeAttribute("user");
//        System.out.println(req.getAttribute("user"));

//        System.out.println(req.getAttribute("usera"));
        req.getRequestDispatcher("/test.jsp").forward(req, resp);
//        resp.sendRedirect("/test.jsp");
//        PrintWriter out = resp.getWriter();
//        String aa = "123456789";
//        System.out.println("asdfghjhgdfsafgh" +  "123456789");
        ;
//        resp.setContentType("text/html;charset=utf-8");
//        out.println("<html>\n" +
//                "<head>\n" +
//                "    <title>Title</title>\n" +
//                "</head>\n" +
//                "<body>\n" +
//                "test\n" +
//                "<div>${username}\n" +
//                "</div>\n" +
//                "<div>${password}</div>\n" +
//                "-------------\n" +
//                "<div>${user}\n" +
//                "</div>\n" +
//                "<div>${pass}</div>\n" +
//                "</body>\n" +
//                "</html>");

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }
}
